/**
* Author: joseph
*/

$(function () {
    init();
});

function init() {
    var slideTrSelector = "#slide-menu-sort table tbody";
    var homeTrSelector = "#home-page-sort table tbody";
    var slideSelectedId = 1;
    var homeSelectedId = 1;

    addSelectOperator();
    //应用按钮,点击后会触发ajax的get请求
    $("#homeApply").click(function () {
        //新的模块序列
        var modulesSort = new Array();
        $("#home-page-sort table tbody tr").each(function (index) {
           // var moduleName = $(this).find("td").eq(1).html();
            var moduleId = $(this).attr("id");
            modulesSort[index] = moduleId;
        });
        $("#home-input").val(JSON.stringify(modulesSort));
        $("#homeNum").val("1");
        // alert("home-submit:" + $("#home-input").val()+"\nwhich tab：" + $("#homeNum").val());
    });
    $("#slideApply").click(function () {
        //新的模块序列
        var modulesSort = new Array();
        $("#slide-menu-sort table tbody tr").each(function (index) {
           // var moduleName = $(this).find("td").eq(1).html();
            var moduleId = $(this).attr("id");
            modulesSort[index] = moduleId;
        });
        $("#slide-input").val(JSON.stringify(modulesSort));
        $("#slideNum").val("2");
        // alert("slide-submit:" + $("#slide-input").val()+"\nwhich tab：" + $("#slideNum").val());
    });

    $("#slideMoveUp, #homeMoveUp").click(function () {
        var flag = true;
        if ($(this).attr("id") == "slideMoveUp") {
            flag = moveUp(slideSelectedId, slideTrSelector);
            if (flag)
                slideSelectedId--;
        }
        else {
            flag = moveUp(homeSelectedId, homeTrSelector);
            if (flag)
                homeSelectedId--;
        }
        addSelectOperator();
    });

    $("#slideMoveDown, #homeMoveDown").click(function () {
        var flag = true;
        var id = $(this).attr("id");
        if (id == "slideMoveDown") {
            flag = moveDown(slideSelectedId, slideTrSelector);
            if (flag)
                slideSelectedId++;
        }
        else {
            flag = moveDown(homeSelectedId, homeTrSelector);
            if (flag)
                homeSelectedId++;
        }
        addSelectOperator();
    });

    //为表格tr绑定点击操作，既是点击一行后，这行颜色会变深，其他行的颜色会变成白色
    function addSelectOperator() {
        $("#home-page-sort table tbody tr").each(function (index) {
            $(this).click(function () {
                homeSelectedId = index;
                $("#home-page-sort table tbody tr").each(function () {
                    $(this).removeClass("selected");
                });
                $(this).addClass("selected");
            })
        });
        $("#slide-menu-sort table tbody tr").each(function (index) {
            $(this).click(function () {
                slideSelectedId = index;
                $("#slide-menu-sort table tbody tr").each(function () {
                    $(this).removeClass("selected");
                });
                $(this).addClass("selected");
            })
        });
    }
}
function moveUp(index, selector) {
    var $trs = $(selector + " tr");
    if (index <= 0) {
        return false;
    }
    //更换两行
    var tmp = $trs[index];
    $trs[index] = $trs[index - 1];
    $trs[index - 1] = tmp;

    //重新生成tr
    $(selector).empty();
    $(selector).append($trs);

    //更新第一列的序号
    $trs = $(selector + " tr");
    $trs.each(function (index) {
        $(this).find("td:first").html(index + 1);
    });
    return true;
}

function moveDown(index, selector) {
    var $trs = $(selector + " tr");
    if (index >= ($trs.length - 1)) {
        return false;
    }
    ;

    //更换两行
    var tmp = $trs[index];
    $trs[index] = $trs[index + 1];
    $trs[index + 1] = tmp;

    //重新生成tr
    $(selector).empty();
    $(selector).append($trs);

    //更新第一列的序号
    $trs = $(selector + " tr");
    $trs.each(function (index) {
        $(this).find("td:first").html(index + 1);
    });
    return true;
}
